import { reactive } from "vue";

interface RegisterUser{
  name: string;
  email: string;
  password_1: string;
  password_2: string;
  role: string;
  code_1:string
  code_2:string
  eName:string

}



export const registerUser = reactive<RegisterUser>({
  name: "aa",
  email: "a@a.com",
  password_1: "123456",
  password_2: "123456",
  role: "user",
  code_1:"",
  code_2:"",
  eName:"",

});



interface RegisterRules {
  
  name: ({
    message: string;
    required: boolean;
    trigger: string;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  })[];
  email: {
    type: string;
    message: string;
    required: boolean;
    trigger: string;
  }[];
  password_1: ({
    required: boolean;
    message: string;
    trigger: string;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  })[];
  password_2: ({
    required: boolean;
    message: string;
    trigger: string;

  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  
  } | {
    validator: (rule: RegisterRules, value: string, callback: any) => void;
    trigger: string;
  })[];
  role: {
  message: string;
  required: boolean;
  trigger: string;
}[];
code_1: ({
  required: boolean;
  message: string;
  trigger: string;
  validator?: undefined;
} | {
  validator: (rule: RegisterRules, value: string, callback: any) => void;
  trigger: string;
  required?: undefined;
  message?: undefined;
})[];
eName: {
  required: boolean;
  message: string;
  trigger: string;
}[];

}



const validatePass_2 = (rule:RegisterRules,value:string ,callback:any) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== registerUser.password_1) {
    callback(new Error("两次输入密码不一致!"));
  } else {
    callback();
  }
};
const validatePass_3 = (rule:RegisterRules,value:string ,callback:any) => {
  if (value === "") {
    callback(new Error("验证码错误！"));
  } else if (value !== registerUser.code_2) {
    callback(new Error("验证码错误！"));
  } else {
    callback();
  }
};
export const registerRules = reactive<RegisterRules>({
  code_1: [
    {
      required: true,
      message: "请输入验证码",
      trigger: "blur",
    },
    { validator: validatePass_3, trigger: "blur" },
  ],
  name: [
    {
      message: "用户名不能为空",
      required: true,
      trigger: "blur",
    },
    {
      min: 2,
      max: 30,
      message: "长度在2到30个字符",
      trigger: "blur",
    },
  ],
  email: [
    {
      type: "email",
      message: "请正确输入邮箱",
      required: true,
      trigger: "blur",
    },
  ],
  password_1: [
    {
      required: true,
      message: "请输入你的密码",
      trigger: "blur",
    },
    {
      min: 6,
      max: 20,
      message: "密码长度在6到20个字符",
      trigger: "blur",
    },
  ],
  password_2: [
    {
      required: true,
      message: "请确认你的密码",
      trigger: "blur",
    },
    {
      min: 6,
      max: 30,
      message: "密码长度在6到20个字符",
      trigger: "blur",
    },
    { validator: validatePass_2, trigger: "blur" },
  ],
  role: [
    {
      message: "请选择你的身份",
      required: true,
      trigger: "blur",
    },
  ],
  
  eName: [
    {
      required: true,
      message: '请输入你所入职的公司',
      trigger: 'change',
    },
  ],

});